package com.example.eventbus.handler

import com.example.eventbus.IEvent
import com.hil.eventbus.Subscription
import java.lang.Exception
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

/**
 * 异步线程处理事件
 * HandlerThread 和普通 Thread 的不同之处在于，普通 Thread 在 run 方法中执行一个耗时任务
 * HandlerThread 在内部创建了消息队列，外界需要听过 Handler 的消息方式来通知 HandlerThread 执行一个具体的任务
 */
class AsyncEventHandler : EventHandler {

    private val excuterService  :ExecutorService = Executors.newCachedThreadPool()
    override fun handleEvent(subscription: Subscription, event: IEvent) {
        excuterService.execute {
            try {
                subscription.targetMethod.invoke(subscription.subscriber.get(), event)
            }catch (e:Exception){

            }
        }
    }
}